package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.MangeMoneyBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MangeMoneyActivity extends AbsActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView title;
    private TextView tvLeftMoney,tvBlockedMoney,tvRecharge,tvWithDraw,tvRenNum,tvBankCardNum,tvIntegral,tvAccountBreakdown,tvWithDrawRecord;
    private MangeMoneyBean moneyBean;
    @Override
    public int setView() {
        return R.layout.activity_mange_money;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().getbalance(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<MangeMoneyBean>>() {
                    @Override
                    public void onData(BaseResponse<MangeMoneyBean> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            moneyBean = value.getData();
                            updateView();
                        }
                    }
                });
    }

    private void updateView() {
        tvLeftMoney.setText(String.valueOf(moneyBean.getBalance()));
        tvBlockedMoney.setText("冻结余额："+String.valueOf(moneyBean.getFreezingBalance()));
        tvRenNum.setText(String.valueOf(moneyBean.getRedEnvelopNum()));
        tvIntegral.setText(String.valueOf(moneyBean.getIntergral()));
        tvBankCardNum.setText(String.valueOf(moneyBean.getBankcardNum()));
    }

    private void initLogic() {
        ivBack.setOnClickListener(this);
        tvBlockedMoney.setOnClickListener(this);
        tvRecharge.setOnClickListener(this);
        tvWithDraw.setOnClickListener(this);
        tvRenNum.setOnClickListener(this);
        tvBankCardNum.setOnClickListener(this);
        tvIntegral.setOnClickListener(this);
        tvAccountBreakdown.setOnClickListener(this);
        tvWithDrawRecord.setOnClickListener(this);
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        title = (TextView) findViewById(R.id.title);
        title.setText("资金管理");

        tvLeftMoney = (TextView) findViewById(R.id.tv_left_money);
        tvBlockedMoney = (TextView) findViewById(R.id.tv_blocked_money);
        tvRecharge = (TextView) findViewById(R.id.tv_recharge);
        tvWithDraw = (TextView) findViewById(R.id.tv_withdraw);
        tvRenNum = (TextView) findViewById(R.id.tv_red_num);
        tvBankCardNum = (TextView) findViewById(R.id.tv_bankcard_num);
        tvIntegral = (TextView) findViewById(R.id.tv_integral);
        tvAccountBreakdown = (TextView) findViewById(R.id.tv_account_breakdown);
        tvWithDrawRecord = (TextView) findViewById(R.id.tv_withdraw_record);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_red_num:
                startActivity(new Intent(this,RedEnvelopActivity.class));
                break;
            case R.id.tv_bankcard_num:
                startActivity(new Intent(this,BankCardListActivity.class));
                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.tv_recharge:
                startActivity(new Intent(this,DepositActivity.class));
                break;
            case R.id.tv_withdraw:
                startActivity(new Intent(this,WithDrawActivity.class));
                break;
            case R.id.tv_account_breakdown:
                startActivity(new Intent(this,AccountBreakdownActivity.class));
                break;
            case R.id.tv_withdraw_record:
                startActivity(new Intent(this,WithDrawRecordActivity.class));
                ToastUtil.showToast("提现记录");
                break;
        }
    }
}
